# coding=utf-8
import os

"""
    文件上传:
    保存log
    系统函数 open  返回文件流
"""
# stream = open("/Users/pengwu/Desktop/hello.txt")

# print(stream.read())

# print(stream.readable())

# print(stream.readline())

# while True:
#     line = stream.readline()
#     if not line:
#         break
#     print(line)

# print(stream.readlines())

"""
    如果是图片格式  需要用二进制的格式读取
"""
# stream = open("/Users/pengwu/Desktop/hello.jpb","rb")


"""
写文件
"""

# stream = open("/Users/pengwu/Desktop/hello.txt", "a")
#
# print(stream.writable())
#
# s = '''
#         预计还要花不少的钱
# '''
# result = stream.write(s)
#
# print(result)
#
# stream.close()

"""
    结合with 能自动释放文件
    复制文件
"""
# with open("/Users/pengwu/Desktop/hello.txt", "rb") as stream:
#     reader = stream.read()
#     with open("/Users/pengwu/Desktop/hello2.txt", "wb") as wstream:
#         wstream.write(reader)

"""
    批量复制
    os 模块
"""


# print(os.path)
# print(os.path.dirname(__file__)) # 当前文件的路径(绝对路径)
# print(type(os.path))
# with open("/Users/pengwu/Desktop/hello.txt", "rb") as reader:
#     path = reader.name
#     file_name = path[path.rfind("/"):]
#     print(file_name)

# 文件名
# path = r"/Users/pengwu/Desktop/hello.txt"
# result = os.path.split(path)
# print(result[1])

# 文件扩展名
# path = r"/Users/pengwu/Desktop/hello.txt"
# r = os.path.splitext(path)
# print(r)


# 是否绝对路径
# r = os.path.isabs("/Users/pengwu/Desktop/hello.txt")
# print(r)

# 获取当前文件的绝对路径
# r = os.path.abspath(__file__)
# print(r)

# r = os.getcwd()
# print(r)

# 文件的大小 字节
# path = r"/Users/pengwu/Desktop/hello.txt"
# r = os.path.getsize(path)
# print(r)

# 拼接目录
# r = os.path.join(os.getcwd(), "file", "a.jpg")
# print(r)

# 文件夹下的所有的文件
# r = os.listdir("/Users/pengwu/Documents/study/pythonStart/file")
# print(r)

# 文件夹是否存在
# r = os.path.exists(r"/Users/pengwu/Documents/study/pythonStart/file")
# print(r)

# 如果没有文件夹  新建文件夹
# if not os.path.exists(r"/Users/pengwu/Documents/study/pythonStart/code"):
#     os.mkdir("/Users/pengwu/Documents/study/pythonStart/code")

# 如果文件夹存在  删除 只能删除空的文件夹
# if os.path.exists(r"/Users/pengwu/Documents/study/pythonStart/code"):
#     os.rmdir("/Users/pengwu/Documents/study/pythonStart/code")

# 删除文件
# os.remove("/Users/pengwu/Documents/study/pythonStart/code/test.py")

# 切换目录
# os.chdir("/Users/pengwu/Documents/study/pythonStart/code")
# print(os.getcwd())


# def copy_dir(__from_path__, __target_path__):
#     """
#     将一个文件夹下的文件全部复制到另外一个文件夹
#     :param __from_path__:  来源文件夹路径
#     :param __target_path__:  目标文件夹路径
#     :return:
#     """
#     if not os.path.exists(__from_path__):  # 判断源文件是否存在
#         return "源文件不存在"
#     if not os.path.isdir(__from_path__):  # 判断参数是否文件夹路径
#         return "源必须是文件夹"
#
#     if not os.path.exists(__target_path__):  # 如果目标文件夹不存在 创建目标文件夹
#         try:
#             os.mkdir(__target_path__)
#         except FileNotFoundError as e:
#             return e
#     else:  # 如果目标路径之前存在 判断是否是文件夹
#         if not os.path.isdir(__target_path__):
#             return "目标必须是文件夹"
#
#     r = os.listdir(__from_path__)  # 获取源文件夹下的文件的名称
#
#     for i in r:  # 循环将文件写到目标文件夹下
#         from_file_path = os.path.join(__from_path__, i)
#         target_file_path = os.path.join(__target_path__, i)
#
#         if not os.path.isdir(from_file_path):  # 判断文件是否是文件夹 如果不是文件夹 复制到目标文件夹下
#             with open(from_file_path, "rb") as stream:
#                 reader = stream.read()
#                 with open(target_file_path, "wb") as writer:
#                     writer.write(reader)
#         else:  # 如果是文件夹 递归自己 创建文件夹 并且将文件复制
#             copy_dir(from_file_path, target_file_path)
#
#     return "复制完成"
#
#
# from_path = "/Users/pengwu/PycharmProjects/pythonProject/file"
# target_path = "/Users/pengwu/PycharmProjects/pythonProject/code"
#
# result = copy_dir(from_path, target_path)
# print(result)

# try:
#     list1 = []
#     list1.pop()
# except Exception as e:
#     print(e)
# finally:
#     print("123")
#
#
# def zw():
#     print("123")


"列表推导式"
